//
//  joinQuestDetailsViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/23.
//

#import "joinQuestDetailsViewController.h"
#import "joinQuestListModel.h"
#import "joinQuestDetailsTableViewCell.h"
#import "joinQuestFooterView.h"

#import "SYAudio.h"


@interface joinQuestDetailsViewController ()<UITableViewDelegate>
@property (nonatomic ,strong) joinQuestListModel * model;
@property (nonatomic ,strong) UIView * buttonview;
@end

@implementation joinQuestDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(170));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:@"交接详情" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    
    
    UIView * buttonview = [UIView new];
    self.buttonview= buttonview;
    buttonview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:buttonview];
    [buttonview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(58) + KSafeAreaBottomHeight);
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#ECECEC");
    [buttonview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(0.5);
    }];
    
    UIButton * qrbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qrbtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(93), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#2892FE") endColor:KcolorHex(@"#0366FE")];
    [qrbtn setTitle:@"确认接收" forState:UIControlStateNormal];
    [qrbtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    qrbtn.titleLabel.font = kCFont(16);
    [DTKTool YJ:kwt(5) andView:qrbtn];
    [qrbtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qrbtn];
    [qrbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(36));
        make.width.offset(kwt(93));
        make.right.offset(kwt(-12));
        make.top.offset(kwt(11));
    }];
    
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(topview.mas_bottom);
        make.bottom.equalTo(buttonview.mas_top);
    }];
    [self loadNewData];
}
- (void)loadNewData{
    [self.view showBusyHUD];
    [SCNetManger PostAuditGetHandoverDetailWithauditId:self.auditId andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){

            self.model = [joinQuestListModel yy_modelWithDictionary:model.result];
           
            if(self.model.button ==NO){
                [self.buttonview mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(0);
                }];
            }
            MJWeakSelf
            self.model.openDurationBlock = ^(NSString * _Nonnull voiceDuration) {
                [weakSelf.tableview reloadData];
            };
            [weakSelf.tableview reloadData];

        } else {
            [self.view showWarning:model.message];
        }
    }];
}
- (void)rightBtnClick{
    ///去人接受
    
    [self.view showBusyHUD];
    [SCNetManger PostAuditConfirmHandoverWithauditId:self.auditId andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            [self.view showWarning:model.message];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 2秒后执行的代码
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    joinQuestFooterView * footerview = [joinQuestFooterView NewView];
    footerview.handoverProcessModel = self.model.handoverProcessModel;
    return footerview;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return self.model.JJFooterH;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.model.JJcellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    joinQuestDetailsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"joinQuestDetailsTableViewCell"];
    if (cell == nil) {
        cell = [[joinQuestDetailsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"joinQuestDetailsTableViewCell"];
    }
    cell.model = self.model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell ;
    
}
- (void)dealloc{
    [[SYAudio shareAudio] audioStop];
}
@end
